#include<bits/stdc++.h>
#define int long long
#define N 500005
using namespace std;
int n;
int a[N],b[N];
int A[4]={0,0,1,-1},B[4]={1,-1,0,0};
int ans;
int viss[N],d[N];
vector<int> g[N];
map<int,map<int,int> > vis;
void bfs(int id){
    for(int i=1;i<=n;i++) viss[i]=0;
    viss[id]=1;d[id]=0;
    queue<int> q;
    q.push(id);
    while(!q.empty()){
        int x=q.front();
        q.pop();
        ans+=d[x];
        for(int i:g[x]){
            if(!viss[i]){
                viss[i]=1;
                d[i]=d[x]+1;
                q.push(i);
            }
        }
    }
}
void sub1(){
    for(int i=1;i<=n;i++) vis[a[i]][b[i]]=i;
    for(int i=1;i<=n;i++){
        int x=a[i],y=b[i];
        for(int j=0;j<4;j++){
            int X=x+A[j],Y=y+B[j];
            if(vis[X][Y]) g[i].push_back(vis[X][Y]);
        }
    }
    for(int i=1;i<=n;i++) bfs(i);
    printf("%lld\n",ans/2);
}
signed main(){
    freopen("game.in","r",stdin);
    freopen("game.out","w",stdout);
    scanf("%lld",&n);
    for(int i=1;i<=n;i++) scanf("%lld %lld",&a[i],&b[i]);
    if(n<=4000){sub1();return 0;}
    int l1=1e9,r1=-1e9,l2=1e9,r2=-1e9;
    for(int i=1;i<=n;i++) l1=min(l1,a[i]),r1=max(r1,a[i]),l2=min(l2,b[i]),r2=max(r2,b[i]);
    int x=r1-l1+1,y=r2-l2+1;
    for(int i=1;i<=x;i++){
        for(int j=1;j<=y;j++){
            vis[i][j]=vis[i-1][j]+vis[i][j-1]-vis[i-1][j-1]+i+j;
            ans+=(i+j)*i*j-vis[i][j];
        }
    }
    vis.clear();
    for(int i=1;i<=x;i++){
        for(int j=y;j>=1;j--){
            vis[i][j]=vis[i-1][j]+vis[i][j+1]-vis[i-1][j+1]+i-j;
            ans+=(i-j)*(i-1)*(y-j)-vis[i-1][j+1];
        }
    }
    printf("%lld\n",ans);
    return 0;
}